package org.jypj.dev.jsonbean;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.jypj.dev.constant.URLConstant;
import org.jypj.dev.model.Train;
import org.jypj.dev.model.TrainStation;
import org.jypj.dev.util.HttpUtils;
import org.jypj.dev.util.PropertyUtil;
import org.jypj.dev.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by ChenYu on 2017-05-10.
 */
public class JsUtil {

    /**
     * 根据车次得到JSResult
     *
     * @param trainId   车次ID
     * @param trainCode 车次
     * @return
     */
    public static List<TrainStation> getJsResultByTrainCode(String trainId, String trainCode) {
        String js_appKey = PropertyUtil.getProperties("Js_appKey");
        trainCode = trainCode.replace("+", "");
        String path = URLConstant.JS_TRAIN_STATION_URL + "?appkey=" + js_appKey + "&trainno=" + trainCode;
        List<TrainStation> trainStations = new ArrayList<>();
        try {
            String s = HttpUtils.sendGetRequestForService(path);
            JSONObject jsonObject = JSON.parseObject(s);
            if ("0".equals(jsonObject.getString("status"))) {
                JsResult jsResult = JSON.parseObject(s, JsResult.class);
                List<JsStation> list = jsResult.getResult().getList();
                if (list != null) {
                    for (JsStation jsStation : list) {
                        String lowUUID = StringUtil.getLowUUID();
                        TrainStation trainStation = new TrainStation();
                        trainStation.setId(lowUUID);
                        trainStation.setTrainId(trainId);
                        trainStation.setTrainCode(trainCode);
                        trainStation.setName(jsStation.getStation());
                        trainStation.setNum(Integer.parseInt(jsStation.getSequenceno()));
                        trainStation.setArriveTime(jsStation.getArrivaltime());
                        trainStation.setDepartTime(jsStation.getDeparturetime());
                        trainStation.setCostTime(jsStation.getCosttime());
                        trainStation.setStopTime(jsStation.getStoptime());
                        trainStation.setIsEnd(jsStation.getIsend());
                        JSONObject lat = getLat(jsStation.getStation());
                        if (lat != null) {
                            String lng1 = lat.getString("lng");
                            String lat1 = lat.getString("lat");
                            trainStation.setLatitude(lat1);
                            trainStation.setLongitude(lng1);
                        }
                        System.out.println(trainStation);
                        trainStations.add(trainStation);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return trainStations;
    }


    /**
     * 根据站点名称得到经纬度
     *
     * @param stationName
     * @return
     */
    public static JSONObject getLat(String stationName) {
        try {
            String path = "http://api.map.baidu.com/geocoder/v2/?address=" + stationName + "&output=json&ak=90kvQxlyt5Pzav1hlp0PUUvCpwl49oGS";
            String json = HttpUtils.sendGetRequestForService(path);
            System.out.println(json);
            JSONObject jsonObject = JSONObject.parseObject(json);
            JSONObject result = jsonObject.getJSONObject("result");
            JSONObject location = result.getJSONObject("location");
            return location;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        /*String lng = location.getString("lng");
        String lat = location.getString("lat");*/
    }
}
